<template>
  <div>
    <!-- el-dialog
    参数--visible="变量"
         :before-close="事件名"--获取文件但还么注册
          slot="title"---修改样式
          show-close="布尔值"--右上角x是否显示
    -->
    <!-- 弹窗 -->
    <el-dialog class="dialogs" width="600px" :visible="isShow" :show-close="true">
      <!-- 头部 -->
      <div class="header" slot="title">用户注册</div>
      <!-- 身体 -->
      <!-- el-form
              属性：model对应数据
                   rules对应prop必选项
                   label-width;对应宽度
                   label-width
      -->
      <el-form :model="form" :rules="rules" label-width="100px">
        <!-- 图片上传 -->
        <el-form-item label="头像" prop="avatar">
          <el-upload
            class="avatar-uploader"
            :action="uploadUrl"
            name="image"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="手机" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="emai">
          <el-input v-model="form.emai"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="图形码" prop="password">
          <el-input v-model="form.emai"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="phone">
          <el-input v-model="form.rcode"></el-input>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isShow: false,
      imageUrl: "",
      uploadUrl: process.env.VUE_APP_URL + "/uploads",
      form: {
        username: "",
        phone: "",
        email: "",
        avatar: "",
        password: "",
        rcode: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入正确格式", trigger: "change" }
        ],

        phone: [
          { required: true, message: "请输入正确格式", trigger: "change" }
        ],
        email: [
          { required: true, message: "请输入正确格式", trigger: "change" }
        ],
        avatar: [
          { required: true, message: "请输入正确格式", trigger: "change" }
        ],
        password: [
          { required: true, message: "请输入正确格式", trigger: "change" }
        ],
        rcode: [
          { required: true, message: "请输入正确格式", trigger: "change" }
        ]
      }
    };
  },
  methods: {
    // 刷新图形码

    // 头像上传功能
    // 成功
    handleAvatarSuccess(res) {
      // this.imageUrl = URL.createObjectURL(file.raw);
      // this.form.avatar = res.data.file_path;
      // 认为触发avatar的表单验证
      // 调用子组件字典的validatafield方法---
      // 参数为数组、字符串，后面借回调函数，error为空，即验证通过，有则提
      // 后面的回调函数可以不写

      // this.imageUrl = "http://127.0.0.1/heimamm/public/" + res.data.file_path;

      this.form.avatar = res.data.file_path;

      this.$refs.form.validateField(["avatar"]);

      this.imageUrl = process.env.VUE_APP_URL + "/" + res.data.file_path;
    },

    // 成功前
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    }
    // 确认按钮

    // 获取验证码
  }
};
</script>

<style lang="less">
.dialogs {
  .el-dialog__header {
    padding: 0;
  }
  .header {
    height: 53px;
    line-height: 53px;
    text-align: center;
    color: white;
    background: linear-gradient(to right, #1494fa, #01c6fa);
  }
  .avatar-uploader {
    margin: 0 auto;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .register-code {
    width: 100%;
    height: 40px;
  }
}
</style>